package com.golang.api.util;

import org.apache.commons.collections4.CollectionUtils;
import org.springframework.util.MultiValueMap;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

public abstract class MultiValueMapUtil {
  public static Map<String, Object> convert(MultiValueMap<String, String> multiValueMap) {
    Map<String, Object> map = new HashMap<>();
    Iterator<Entry<String, List<String>>> iterator = multiValueMap.entrySet().iterator();
    while (iterator.hasNext()) {
      Entry<String, List<String>> entry = iterator.next();
      String paramName = entry.getKey();
      List<String> paramValue = entry.getValue();
      if (CollectionUtils.isEmpty(paramValue)) {
        map.put(paramName, "");
      }
      if (CollectionUtils.isNotEmpty(paramValue) && paramValue.size() == 1) {
        map.put(paramName, paramValue.get(0));
      }
      if (CollectionUtils.isNotEmpty(paramValue) && paramValue.size() > 1) {
        map.put(paramName, paramValue);
      }
    }
    return map;
  }
}
